﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using UcAsp.WorkFrame;
using UcAsp.Models;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
namespace UcAsp.Controllers
{
    public class AdminController : Controller
    {
        //
        // GET: /Admin/

        public ActionResult Index()
        {
            if (Session["Info"] == null)
            {

                return Redirect("/adminlogin/login");
            }
            else
            {
                if (Session["Info"].ToString() != "Admin")
                {
                    return Redirect("/adminlogin/login");
                }
            }
            return View();
        }
        public ActionResult SetSetting()
        {
            if (Session["Info"] == null)
            {

                return Redirect("/adminlogin/login");
            }
            else
            {
                if (Session["Info"].ToString() != "Admin")
                {
                    return Redirect("/adminlogin/login");
                }
            }
            SiteSetting site = SettingConfig.GetSiteSetting;
            return View(site);
        }
        [ValidateInput(false)]
        public ActionResult SaveSetSetting(SiteSetting model)
        {
            SettingConfig.SaveSiteSetting(model);
            Message message = new Message { Result=true,Messages="信息完成更新" };
            return Json(message,JsonRequestBehavior.AllowGet);
        }
        public ActionResult SetAdvPostion()
        {
            List<UcAsp.Models.AdvPositions> list = WorkFrame.AdvPositions.GetListAdvPositions;
            return View(list); 
        }
        public ActionResult GetAdvPostion()
        {
            string name = Request.Form["AdvName"];
            UcAsp.Models.AdvPositions adv = WorkFrame.AdvPositions.GetAdvPositions(name);
            return View(adv);
        }
        [ValidateInput(false)]
        public ActionResult SaveAdvPostion()
        {
            UcAsp.Models.AdvPositions adv = new Models.AdvPositions { AdvHtml = Request.Form["AdvHtml"], AdvChinaName = Request.Form["AdvChinaName"], AdvPositionName = Request.Form["AdvName"] };
            WorkFrame.AdvPositions.SaveAdvPositions(adv);
            Message message = new Message { Result = true, Messages = "信息完成更新" };
            return Json(message, JsonRequestBehavior.AllowGet);
        }
        public ActionResult UpFile()
        {
            string savepath = Request.QueryString["Path"];
            HttpFileCollectionBase file = Request.Files;
            string filename = string.Empty;
            string path = Server.MapPath("/") + "\\content\\";
            string ourpath = "/Content/";
            string name = DateTime.Now.Ticks.ToString();
            if (savepath != "")
            {
                path = path + "\\" + savepath+"\\";
                ourpath = ourpath +  savepath+"/";
            }
            if (!Directory.Exists(path))
            {
                Directory.CreateDirectory(path);
            }
            for (int i = 0; i < file.Count; i++)
            {
                if (file[i].ContentType.IndexOf("image") > -1)
                {
                    string ext = file[i].FileName.Split('.')[file[i].FileName.Split('.').Length - 1];
                    if (file[i].ContentLength > 200)
                    {
                        
                        file[i].SaveAs(path + name+"."+ext);
                        if (savepath == "userheader")
                        {
                            SaveSmallImage(path + name + "." + ext, path + name + "header." + ext);
                            try{
                                System.IO.File.Delete(path + name + "." + ext); 
                            }
                            catch{}
                            filename = name + "header." + ext;
                        }
                        else
                        {
                            filename = name + "." + ext;
                        }
                    }
                }
            }
            Message message = new Message { Result = true, Note1 = filename, Messages = "上传成功" };
            if (filename == "")
            {
                return Content("{error: '',msg: ''}");
            }
            else
            { return Content("{error: '',msg: '" + ourpath + filename + "'}"); }
            
        }
        public ActionResult HomeContent()
        {
            if (Session["Info"] == null)
            {

                return Redirect("/adminlogin/login");
            }
            else
            {
                if (Session["Info"].ToString() != "Admin")
                {
                    return Redirect("/adminlogin/login");
                }
            }
            return View();
        }
        public string SaveSmallImage(string filepath,string outputpath)
        {
            Image img = Image.FromFile(filepath);
            img.GetThumbnailImage(30, 30, new Image.GetThumbnailImageAbort(ThumbnailCallback), IntPtr.Zero);
            img.Save(outputpath, ImageFormat.Jpeg);
            img.Dispose();
            return filepath;
        }
        public bool ThumbnailCallback()
        {
            return false;
        }
    }
}
